package org.gd136.ce.stage;

import java.util.ArrayList;
import java.util.List;

import org.gd136.ce.action.CallAction;
import org.gd136.ce.actor.ShaderGroup;
import org.gd136.ce.game.GS;
import org.gd136.ce.listener.ButtonListener;
import org.gd136.ce.manager.RM;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class TitleLayer extends Layer {

	Texture boxTexture;
	Image boxImage;

	FrameBuffer fbo;
	ShaderProgram shader;
	ShaderGroup shaderGroup;
	ShapeRenderer shape;

	public TitleLayer() {
		super();

		initBoxs();
		// /
		shape = new ShapeRenderer();
		addDisposeSign(shape);

		fbo = new FrameBuffer(Format.RGBA8888, GS.width, GS.height, false,
				true);
		addDisposeSign(fbo);
		Image efBg = new Image(new TextureRegion(fbo.getColorBufferTexture(), 0,
				fbo.getHeight(), fbo.getWidth(), -fbo.getHeight()));
		efBg.addAction(new Action() {
			@Override
			public boolean act(float delta) {
				for (Box b : boxs) {
					b.y += b.speed * delta;
					if (b.y > getHeight()) {
						b.y = -50;
					}
				}
				drawEFBG();
				return false;
			}
		});

		drawEFBG();

		shader = new ShaderProgram(RM.res("shader/default.vert"),
				RM.res("shader/border.fgt"));
		addDisposeSign(shader);

		shaderGroup = new ShaderGroup(shader) {
			@Override
			public void setUniform(ShaderProgram shader) {
			}
		};
		// //////
		Image bg = image("ui/bg.png");
		addActor(bg);

		shaderGroup.addActor(efBg);
		addActor(shaderGroup);

		Image pic = image("ui/bg_pic.png");
		addActor(pic);

		Image title = image("ui/title_logo.png");
		addActor(title);
		title.setPosition(100, 600);

		Image btnStart = image("ui/start.png");
		addActor(btnStart);
		btnStart.setPosition(877, 455);
		btnStart.addListener(ButtonListener.create(() -> {

		}));

		Image btnSaveLoad = image("ui/save_load.png");
		addActor(btnSaveLoad);
		btnSaveLoad.setPosition(952, 366);
		btnSaveLoad.addListener(ButtonListener.create(() -> {

		}));

		Image btnCG = image("ui/cg.png");
		addActor(btnCG);
		btnCG.setPosition(904, 277);
		btnCG.addListener(ButtonListener.create(() -> {

		}));

		Image btnConfig = image("ui/setting.png");
		addActor(btnConfig);
		btnConfig.setPosition(860, 188);
		btnConfig.addListener(ButtonListener.create(() -> {
		}));

		Image btnQuit = image("ui/exit.png");
		addActor(btnQuit);
		btnQuit.setPosition(995, 99);
		btnQuit.addListener(ButtonListener.create(() -> {
			BlackLayer mask = new BlackLayer();
			mask.getMask().getColor().a = 0;
			addActor(mask);
			mask.getMask().addAction(Actions.sequence(Actions.fadeIn(0.3f),
					CallAction.create(() -> {
				Gdx.app.exit();
			})));
		}));

		// ////////

	}

	// @Override
	// public void render() {
	//
	// super.render();
	// }

	private class Box {
		float x, y;
		float width;
		Color color;
		float speed;
	}

	List<Box> boxs = new ArrayList<>();

	private void initBoxs() {
		Pixmap pix = new Pixmap(20, 20, Format.RGBA8888);
		pix.setColor(boxColor);
		pix.fillRectangle(0, 0, 20, 20);
		boxTexture = new Texture(pix);
		pix.dispose();
		addDisposeSign(boxTexture);
		boxImage = new Image(boxTexture);

		for (int i = 0; i < 1000; i++) {
			Box b = new Box();
			b.x = (float) (Math.random() * getWidth());
			b.y = (float) (Math.random() * getHeight());
			b.width = (float) (Math.random() * 15 + 7);
			b.speed = (float) (Math.random() * 60 + 10);
			float m = (float) (Math.random() * 0.8 - 0.4);
			b.color = boxColor.cpy();
			b.color.add(m * 0.3f, m * 0.6f, m * 0.1f, 0);
			// b.color.a = (float) (Math.random() * 0.7 + 0.2);
			boxs.add(b);
		}
	}

	Color boxColor = new Color(64f / 255, 195f / 255, 235f / 255, 1f);

	private void drawEFBG() {
		if (getStage() == null) {
			return;
		}

		fbo.begin();
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		// shape.begin(ShapeType.Filled);
		// for (Box b : boxs) {
		// shape.setColor(b.color);
		// shape.rect(b.x, b.y, b.width, b.width);
		// }
		//
		// shape.end();

		getStage().getBatch().begin();
		// int oldDst = getBatch().getBlendDstFunc();
		// int oldSrc = getBatch().getBlendSrcFunc();
		// getBatch().setBlendFunction(GL20.GL_SRC_ALPHA,
		// GL20.GL_DST_COLOR);
		for (Box b : boxs) {
			boxImage.setSize(b.width, b.width);
			boxImage.setPosition(b.x, b.y);
			boxImage.setColor(b.color);
			boxImage.getColor().a *= Math.min(1,
					(1 - (b.y / getHeight() / 1.3f)));
			boxImage.draw(getStage().getBatch(), 1);
		}

		boxImage.setSize(410, getHeight());
		boxImage.setPosition(825, 0);
		boxImage.setColor(Color.WHITE);
		boxImage.getColor().a = 0.3f;
		boxImage.draw(getStage().getBatch(), 1);

		// getBatch().setBlendFunction(oldSrc, oldDst);
		getStage().getBatch().end();
		fbo.end();
	}
}
